﻿using System;
using System.Windows.Forms;
using System.Xml;
using KTD_XSMS.Properties;
using KTDSMS;

namespace KTD_XSMS
{
    public partial class ChangePass : Form
    {
        XmlDocument Doc = new XmlDocument();
        public ChangePass()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Dispose();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            StringHash hash = new StringHash();
           
            if (txtNewPass.Text.Trim() != txtReNewPass.Text.Trim())
            {
                Unity.Message("New Pass and Re-New Pass is different");
                txtReNewPass.Focus();
                return;
            }
            if (txtNewPass.Text.Trim() == "")
            {
                Unity.Message("Please enter New Pass");
            }
           
            if (hash.Encrypt(txtOldPass.Text.Trim()) != Settings.Default.LoginPass)
            {
                Unity.Message("Passwords do not exactly");
                txtOldPass.Focus();
                return;
            }
            Doc.Load(Application.StartupPath + "\\Login.xml");
            XmlNode node = Doc.SelectSingleNode("/Logins/Login[User='" + Settings.Default.LoginUser + "']");
            
            if (node != null)
            {
                node.ChildNodes[1].InnerText = hash.Encrypt(txtNewPass.Text.Trim());
            }
            Doc.Save(Application.StartupPath + "\\Login.xml");
            Unity.Message("Change Password Successfull");
            base.Dispose();
        }

        private void txtOldPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtNewPass.Focus();
            }
        }

        private void txtNewPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtReNewPass.Focus();
            }
        }

        private void txtReNewPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnSave_Click(sender,e);
            }
        }

        private void ChangePass_Load(object sender, EventArgs e)
        {
            txtOldPass.Focus();
        }
    }
}
